package de.geckocraft.GeckoEventBroadcaster.Main;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

public class AutoBroadcast
{
  private static boolean caststartet = false;

  public static void autocast()
  {
    long random = Math.round(Math.random() * 199.0D) + 1L;

    BukkitTask task = new checkcast(Main.getMain(), null).runTaskLater(Main.getPlugin(), random);
  }

  private static class cast extends BukkitRunnable
  {
    private Main plugin;

    private cast(Main plugin)
    {
      this.plugin = plugin;
    }

    public void run()
    {
      AutoBroadcast.caststartet = false;

      Date eventdate = (Date)EventList.Eventdate.get(EventList.Eventnummbers.get(0));
      String eventname = (String)EventList.Eventname.get(EventList.Eventnummbers.get(0));
      Date now = new Date();
      long diferensSek = (eventdate.getTime() - now.getTime()) / 1000L;
      long stundenSek = eventdate.getHours() * 60 * 60;
      long minutenSek = eventdate.getMinutes() * 60;
      long diferensDaySek = diferensSek - stundenSek - minutenSek;

      if (diferensDaySek < 0L)
      {
        if (diferensSek * -1L > MainConfig.getchangetext())
        {
          String time = eventdate.getHours() + ":" + eventdate.getMinutes();
          Bukkit.getServer().broadcastMessage(Template.parseMessage("today", new String[] { time, eventname }));
        }
        else
        {
          long stundenchach = (diferensSek + 1L) / 60L / 60L;
          long minutenchach = (diferensSek + 1L) / 60L;
          long diferensDaySekchach = diferensSek + 1L;

          while (diferensDaySekchach >= 3600L)
          {
            diferensDaySekchach -= 3600L;
            minutenchach = diferensDaySekchach / 60L;
          }

          String stunden = stundenchach;
          String min = minutenchach;

          if (minutenchach == 0L)
          {
            Bukkit.getServer().broadcastMessage(Template.parseMessage("todayh", new String[] { stunden, eventname }));
          }
          else if (stundenchach == 0L)
          {
            Bukkit.getServer().broadcastMessage(Template.parseMessage("todaymin", new String[] { min, eventname }));
          }
          else
          {
            Bukkit.getServer().broadcastMessage(Template.parseMessage("todayhmin", new String[] { stunden, min, eventname }));
          }
        }
      }
      else if (diferensDaySek < 86400L)
      {
        String time = eventdate.getHours() + ":" + eventdate.getMinutes();
        Bukkit.getServer().broadcastMessage(Template.parseMessage("today", new String[] { time, eventname }));
      }
      else
      {
        String datum = eventdate.getDay() + "." + eventdate.getMonth() + "." + eventdate.getYear();
        String time = eventdate.getHours() + ":" + eventdate.getMinutes();
        Bukkit.getServer().broadcastMessage(Template.parseMessage("longer", new String[] { datum, time, eventname }));
      }
    }
  }

  private static class checkcast extends BukkitRunnable
  {
    private Main plugin;

    private checkcast(Main plugin)
    {
      this.plugin = plugin;
    }

    public void run()
    {
      int intervallstart = MainConfig.getIntervallstart();
      int intervall = MainConfig.getintervall();
      int intervallstop = MainConfig.getintervallstop();

      if (!EventList.Eventdate.isEmpty())
      {
        Date eventdate = (Date)EventList.Eventdate.get(EventList.Eventnummbers.get(0));

        Date now = new Date();
        int nowsek = (int)(now.getTime() / 1000L);

        if (eventdate.getTime() < now.getTime())
        {
          EventList.removeEvent(((Integer)EventList.Eventnummbers.get(0)).intValue());

          BukkitTask localBukkitTask1 = new checkcast(Main.getMain()).runTaskLater(Main.getPlugin(), 0L);
        }
        else
        {
          if (eventdate.getTime() / 1000L - intervallstop * 60 > nowsek)
          {
            int nextbroadcast = -1;

            int i = 1;
            int calsek = (int)(eventdate.getTime() / 1000L);

            while (calsek > nowsek)
            {
              nextbroadcast = calsek - nowsek;
              calsek = (int)(eventdate.getTime() / 1000L - i * intervall * 60);
              if (i * intervall > intervallstart)
              {
                calsek = nowsek + 1;
                nextbroadcast = -1;
              }
              i++;
            }
            if (nextbroadcast > 0)
            {
              if (nextbroadcast < 11)
              {
                if (!AutoBroadcast.caststartet)
                {
                  BukkitTask task = new AutoBroadcast.cast(Main.getMain(), null).runTaskLater(Main.getPlugin(), nextbroadcast * 20);
                  AutoBroadcast.caststartet = true;
                }
              }
            }
          }
          else
          {
            ArrayList zusatzList = MainConfig.getZusatzList();
            for (int i = 0; i < zusatzList.size(); i++)
            {
              int zusatzintervall = ((Integer)zusatzList.get(i)).intValue();

              int calsek = (int)(eventdate.getTime() / 1000L - zusatzintervall * 60);

              if (calsek > nowsek)
              {
                int nextbroadcast = calsek - nowsek;
                if (nextbroadcast < 11)
                {
                  if (!AutoBroadcast.caststartet)
                  {
                    BukkitTask task = new AutoBroadcast.cast(Main.getMain(), null).runTaskLater(Main.getPlugin(), nextbroadcast * 20);
                    AutoBroadcast.caststartet = true;
                  }
                  i = zusatzList.size();
                }
                else
                {
                  i = zusatzList.size();
                }
              }
            }
          }
          AutoBroadcast.autocast();
        }
      }
      else
      {
        AutoBroadcast.autocast();
      }
    }
  }
}